import { ethers } from "ethers";

const INFO = {
  tokenabi: [
    "function transfer( address recipient, uint256 amount ) external returns (bool)",
    "function allowance(address owner, address spender) external view returns (uint256)",
    "function approve(address spender, uint256 amount) external returns (bool)",
    "function balanceOf(address account) external view returns (uint256)",
    "function symbol() external view returns (string memory)",
    "function decimals() external pure returns (uint8)",
  ],
};
//获取BNB
const getBNBalance = async (useAccount) => {
  const rpcUrl = "https://rpc1.wwwrfs.com/"; // 以太坊节点的RPC地址
  const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
  const balance = await provider.getBalance(useAccount);
  return ethers.utils.formatEther(balance);
};

const getyuBalance = async (useAccount, tokenAddress, decimals) => {
  try {
    const rpcUrl = "https://rpc1.wwwrfs.com/"; // 以太坊节点的RPC地址
    // 创建以太坊provider
    const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
    const token = new ethers.Contract(tokenAddress, INFO.tokenabi, provider);
    const bal = await token.balanceOf(useAccount);
    return ethers.utils.formatUnits(bal, decimals);
  } catch (e) {
    return 0.0;
  }
};

export { getyuBalance, getBNBalance };
